package com.soft.bams.dao;

import com.soft.bams.entity.Account;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/5/9
 */
public class FileDao implements IDao<Account, List<Account>>{
    private Long currentId;

    public FileDao() {
        this.currentId = readCurrentIdFromFile();
        if (this.currentId == null) {
            currentId = 100000000000000000L;
            writeCurrentIdToFile(this.currentId);
        }
    }

    @Override
    public Long getId() {
        return currentId;
    }

    @Override
    public void setId(Long id) {
        this.currentId = id;
    }

    @Override
    public Long createId() {
        Long currentId = readCurrentIdFromFile();
        if (currentId == null) {
            System.out.println("无法读取ID文件，使用初始ID");
            currentId = 100000000000000000L;
        }

        this.setId(currentId);
        Long newId = IDao.super.createId();

        // 更新文件中的id
        if (newId != -1L) {
            writeCurrentIdToFile(this.getId());
        }

        return newId;
    }

    private Long readCurrentIdFromFile() {
        File file = new File("id.txt");

        if (!file.exists() || file.length() == 0) {
            return null;
        }
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
            return (Long) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    private void writeCurrentIdToFile(Long id) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("id.txt"))) {
            oos.writeObject(id);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean insert(Account account) {
        List<Account> accounts = selectAll();
        accounts.add(account);
        return saveAllAccounts(accounts);
    }

    @Override
    public Account selectOne(Long id, String password) {
        return selectAll().stream()
                .filter(acc -> acc != null && acc.getId().equals(id) && acc.getPassword().equals(password))
                .findFirst()
                .orElse(null);
    }

    @Override
    public Account selectOne(Long id) {
        return selectAll().stream()
                .filter(acc -> acc != null && acc.getId().equals(id))
                .findFirst()
                .orElse(null);
    }

    @Override
    public List<Account> selectAll() {
        File file = new File("accounts.txt");

        if (!file.exists() || file.length() == 0) {
            return new ArrayList<>();
        }

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
            return (List<Account>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    private boolean saveAllAccounts(List<Account> accounts) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("accounts.txt"))) {
            oos.writeObject(accounts);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(Account account) {
        List<Account> accounts = selectAll();
        for (int i = 0; i < accounts.size(); i++) {
            if (accounts.get(i).getId().equals(account.getId())) {
                accounts.set(i, account);
                return saveAllAccounts(accounts);
            }
        }
        return false;
    }

    /*public boolean delete(Long id) {
        List<Account> accounts = selectAll();
        // 查找并删除对应的账户
        for (int i = 0; i < accounts.size(); i++) {
            if (accounts.get(i).getId().equals(id)) {
                accounts.remove(i);
                return saveAllAccounts(accounts);
            }
        }
        return false;
    }*/
}
